<?php

namespace App\Http\Controllers\Reception\Resources;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\URL;
use Illuminate\Http\Request;

//资源分类模块
class ResourcesCateController extends Controller
{
    //获取文章分类列表
    public function getResourcesCateList(Request $request){
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifytokens($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $info=DB::table('resources_cate')
        ->leftjoin('upload_file', 'resources_cate.fileid', '=', 'upload_file.fileid')
        ->select('resources_cate.*', 'upload_file.file_name', 'upload_file.url')
        ->get();
        if($info){
            $info=json_decode($info,true);
            foreach ($info as $key => $value) {
                $info[$key]['url'] =  http_type() . $_SERVER['HTTP_HOST'] . "/" . $value['url'] . "/" . $value['file_name'];
            }
            $data=success();
            $data['data']=$info;
        }else{
            $data=fail();
        }
        return $data;
    }

    //获取文章分类（筛选完二级分类）
    public function getresourcesCate(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifytokens($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $info = DB::table('resources_cate')
            ->leftjoin('upload_file', 'resources_cate.fileid', '=', 'upload_file.fileid')
            ->orderBy('resources_catid', 'desc')
            ->get();
        if ($info) {
            $info = json_decode($info, true);
            foreach ($info as $key => $value) {
                $info[$key]['resources_cate_time'] = date('Y-m-d', $value['resources_cate_time']);
                $info[$key]['url'] =  http_type() . $_SERVER['HTTP_HOST'] . "/" . $info[$key]['url'] . "/" . $info[$key]['file_name'];
                foreach ($info as $k => $v) {
                    if ($value['resources_catid'] == $v['parentid']) {
                        $info[$key]['children'][$k] = $v;
                        $info[$key]['children'] = array_values($info[$key]['children']);
                    }
                }
            }
            foreach ($info as $keys => $values) {
                if ($values['parentid'] != '0') {
                    unset($info[$keys]);
                }
            }
            $info = array_values($info);

            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }


    // //获取文章分类状态
    // public function getResourcesCateStatus(Request $request){
    //     $token = $request->query('token');
    //     //验证权限
    //     $verifytoken = verifytokens($token);
    //     if (isset($verifytoken)) {
    //         return $verifytoken;
    //     }
    //     $map['status_id']=1;
    //     $info=DB::table('status')->where($map)->first();
    //     if($info){
    //         $data=success();
    //         $data['data']=$info;
    //     }else{
    //         $data=fail();
    //     }
    //     return $data;
    // }
}